#include "barrageedit.h"
#include "model/datacenter.h"
#include "toast.h"

BarrageEdit::BarrageEdit(QWidget *parent)
    : QLineEdit{parent}
{
    this->setFixedSize(1030, 32);
    this->setPlaceholderText("这个视频在说的是什么...");
    this->setMaxLength(30); // 最多输⼊30个字符
    this->setTextMargins(12, 6, 0, 7); // 左 上 右 下

    // 初始化发送按钮
    sendBSBtn = new QPushButton(this);
    sendBSBtn->setFixedSize(58, 28);
    sendBSBtn->setText("发送");
    sendBSBtn->setCursor(QCursor(Qt::ArrowCursor));
    sendBSBtn->setStyleSheet("background-color : #3ECEFE;"
                             "border-radius : 4px;"
                             "color : #FFFFFF;"
                             "font-size : 14px;"
                             "font-family : 微软雅⿊;");
    sendBSBtn->move(width()-sendBSBtn->width() - 2, 2);

    // 发送弹幕按钮点击
    connect(sendBSBtn, &QPushButton::clicked, this, &BarrageEdit::onSendBulletScreenBtn);
    connect(this, &QLineEdit::returnPressed, this, [=]{
        QString text = this->text();
        this->clear();
        emit sendBulletScreen(text);
    });

    login = new Login();
    connect(login, &Login::loginSuccess, this, [=]{
        setPlaceholderText("发个友善的弹幕~~~");
        sendBSBtn->setText("发送");
    });
}

void BarrageEdit::mousePressEvent(QMouseEvent *event)
{
    // 当用户点击弹幕编辑框时，需要先检测用户是否登录
    // 如果是临时用户，需要提示让用户登录
    auto dataCenter = model::DataCenter::getInstance();
    auto myselfInfo = dataCenter->getMyselfInfo();
    if(myselfInfo->isTempUser()){
        if(placeholderText() != "请先登录或注册"){
            setPlaceholderText("请先登录或注册");
            sendBSBtn->setText("登录");
        }else{
            Toast::showMessage("请先登录或注册", login);
        }
    }
}

void BarrageEdit::onSendBulletScreenBtn()
{
    QString text = this->text();
    this->clear();
    emit sendBulletScreen(text);
}
